﻿using System.Collections.Generic;
using UnityEngine;
using FairyGUI;

public class FCardListMediator : MyWindow {
    GList _list;
    FEditCardPanel _editCardPanel;
    FCardTypePopup _cardTypePopup;
    FCardTypePopup _skillTypePopup;
    FCardTypePopup _roleTypePopup;
    GComponent _cardList;
    GTextField _cardNum;

    static List<Card> _cardDataList;//所有卡牌数据
    static List<Card> _curCardDataList;//筛选后的卡牌数据
    static FCardListMediator _instance;
    OneDelegate _callback;
    GComponent _content;


    protected override void OnInit(){
        base.OnInit();
        this.contentPane = UIPackage.CreateObject("CardPackage", "CardList").asCom;
        _cardList = this.contentPane;
        InitPanel();
    }

    public FCardListMediator(OneDelegate cb = default(OneDelegate))
    {
        _instance = this;

        SetCallBack(cb);
    }

    public void SetCallBack(OneDelegate cb)
    {
        _callback = cb;
        FreshCallBackPanel();
    }

    private void FreshCallBackPanel(){
        if (_cardList == null) return;
        var closeBtn = _cardList.GetChild("closeBtn");
        if(_callback != null)
        {
            closeBtn.onClick.Add(OnCloseBtn);
            closeBtn.visible = true;
        }else{
            closeBtn.visible = false;
        }
    }

    private void InitPanel()
    {
        FreshCallBackPanel();
        _cardNum = _cardList.GetChild("numText").asTextField;
        _cardList.GetChild("newBtn").onClick.Add(OnNewBtnClick);
        var cardTypeBtn = _cardList.GetChild("cardTypeBtn");
        var skillTypeBtn = _cardList.GetChild("skillTypeBtn");
        var roleTypeBtn = _cardList.GetChild("roleTypeBtn");
        cardTypeBtn.onClick.Add(OnTypeBtnClick);
        skillTypeBtn.onClick.Add(OnTypeBtnClick);
        roleTypeBtn.onClick.Add(OnTypeBtnClick);
        cardTypeBtn.visible = true;
        skillTypeBtn.visible = true;
        roleTypeBtn.visible = true;
        _list = _cardList.GetChild("cardList").asList;
        //编辑卡牌面板
        _editCardPanel = (FEditCardPanel)FUIMain.GetWindow(GMConfig.PanelType.EditCardPanel,null);
        //卡牌类型选择
        UIObjectFactory.SetPackageItemExtension("ui://CardPackage/CardTypePopup", typeof(FCardTypePopup));
        _cardTypePopup = (FCardTypePopup)UIPackage.CreateObject("CardPackage", "CardTypePopup");
        _skillTypePopup = (FCardTypePopup)UIPackage.CreateObject("CardPackage", "CardTypePopup");
        _roleTypePopup = (FCardTypePopup)UIPackage.CreateObject("CardPackage", "CardTypePopup");

        InitData();
    }
    private static void InitData()
    {
        if (_cardDataList != null) return;
        _cardDataList = CardMO.GetCardList();
    }
	// Use this for initialization
    protected override void OnShown()
    {         
        //设置item单机事件监听
        _list.onClickItem.Add(OnCardListClick);
        _list.itemRenderer = RenderListItem;

        //赋值
        var typeDict = _editCardPanel.GetTypeDict();
        _cardTypePopup.InitData(typeDict.GetValue("typeTextList"),FreshCardList);
        _skillTypePopup.InitData(typeDict.GetValue("skillTextList"), FreshCardList);
        _roleTypePopup.InitData(typeDict.GetValue("roleTextList"), FreshCardList);

        FreshCardList();
    }

    void RenderListItem(int index, GObject item)
    {
        var btn = (FCard)item;
        btn.InitData(_curCardDataList[index]);
    }

    private void OnCardListClick(EventContext context)
    {
        var index = _list.GetChildIndex((GObject)context.data);
        if (_callback != null)
        {
            _callback(_curCardDataList[index].id);
        }
        else
        {
            _editCardPanel.Show();
            //获取对应所有卡牌内的下标，以便后面保存
            var _index = _cardDataList.IndexOf(_curCardDataList[index]);
            _editCardPanel.InitData(_curCardDataList[index], _index);
        }
    }
    /// <summary>
    /// 新建卡牌
    /// </summary>
    private void OnNewBtnClick()
    {
        //创建一张新卡
        var _card = new Card();
        int lastId = 0;
        if(_cardDataList.Count != 0)
        {
            var lastCard = _cardDataList[_cardDataList.Count - 1];
            lastId = System.Convert.ToInt32(lastCard.id.Split('_')[1]);
        }
        _card.id = "Card_"+(lastId + 1);
        CardMO.ResetCardName(_card);
        _editCardPanel.Show();
        _editCardPanel.InitData(_card,_cardDataList.Count);
    }
    /// <summary>
    /// 类型筛选
    /// </summary>
    /// <param name="context"></param>
    private void OnTypeBtnClick(EventContext context)
    {
        var sender = (GComponent)context.sender;

        switch (sender.name)
        {
            case "cardTypeBtn":
                GRoot.inst.ShowPopup(_cardTypePopup, sender);
                break;
            case "skillTypeBtn":
                GRoot.inst.ShowPopup(_skillTypePopup, sender);
                break;
            case "roleTypeBtn":
                GRoot.inst.ShowPopup(_roleTypePopup, sender);
                break;
        }
    }
    /// <summary>
    /// 保存卡牌列表
    /// </summary>
    public static void SaveCardDataList(){
        CardMO.SaveCardDataList();
    }
    /// <summary>
    /// 保存卡牌
    /// </summary>
    /// <param name="cardData"></param>
    /// <param name="index"></param>
    /// <returns></returns>
    public static bool SaveCard(Card cardData,int index)
    {
        CardMO.SaveCard(cardData,index);
        if(_instance != null) _instance.FreshCardList();
        return true;
    }
    /// <summary>
    /// 删除卡牌
    /// </summary>
    /// <param name="index"></param>
    /// <returns></returns>
    public static bool DelCard(int index){
        if (index < 0 || index >= _cardDataList.Count) return false;
        CardMO.DelCard(index);
        if(_instance != null) _instance.FreshCardList();
        return true;
    }
    /// <summary>
    /// 刷新卡牌面板
    /// </summary>
    private void FreshCardList(){
        var cardTypeSelectedList = _cardTypePopup.GetSelectedList();
        var skillTypeSelectedList = _skillTypePopup.GetSelectedList();
        var roleTypeSelectedList = _roleTypePopup.GetSelectedList();

        _curCardDataList = new List<Card>();

        for(int j = 0;j < _cardDataList.Count; j++)
        {
            //卡牌类型筛选
            var cardTypeOk = cardTypeSelectedList.Count == 0 || cardTypeSelectedList.Contains((int)_cardDataList[j].type);
            //技能类型
            var skillTypeOk = skillTypeSelectedList.Count == 0 || skillTypeSelectedList.Contains((int)_cardDataList[j].skillType);
            var roleTypeOk = roleTypeSelectedList.Count == 0;
            //角色类型筛选
            for (int i = 0;i < _cardDataList[j].roles.Count; i++)
            {
                if (roleTypeSelectedList.Contains((int)_cardDataList[j].roles[i]))
                {
                    roleTypeOk = true;
                    break;
                }
            }
            if (cardTypeOk && skillTypeOk && roleTypeOk) _curCardDataList.Add(_cardDataList[j]);
        }
        //全部都没选择
        if(cardTypeSelectedList.Count == 0 && skillTypeSelectedList.Count == 0 && roleTypeSelectedList.Count == 0)
        {
            _curCardDataList = _cardDataList;
        }
        // _list.RemoveChildrenToPool();
        _list.numItems = _curCardDataList.Count;
        _cardNum.text = _curCardDataList.Count.ToString();
    }
	// Update is called once per frame
	void Update () {
		
	}
    /// <summary>
    /// 关闭面板
    /// </summary>
    private void OnCloseBtn()
    {
        if (_callback != null) _callback(0);
    }
}
